package com.smedi.ismedi.inmail.core.port.adapter.restful;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smedi.ismedi.inmail.api.dto.request.MessagesSendRequest;
import com.smedi.ismedi.inmail.api.dto.request.QueryListMessageRecordRequest;
import com.smedi.ismedi.inmail.api.dto.request.QueryPageMessageRecordRequest;
import com.smedi.ismedi.inmail.api.dto.response.MessageRecordResponse;
import com.smedi.ismedi.inmail.api.feign.RemoteMessageFeignApi;
import com.smedi.ismedi.inmail.core.application.command.MessageInfoCommand;
import com.smedi.ismedi.inmail.core.application.query.MessageQueryService;
import com.smedi.ismedi.inmail.core.application.query.vo.MessageRecordVo;
import com.smedi.ismedi.inmail.core.application.service.MessageApplicationService;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.MessageRecord;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author u
 */
@RestController
@AllArgsConstructor
public class MessageController implements RemoteMessageFeignApi {
    private final DozerUtil dozerUtil;

    private final MessageApplicationService messageApplicationService;

    private final MessageQueryService messageRecordQueryRepository;

    @Override
    public ResponseData<List<MessageRecordResponse>> postSendMessage(MessagesSendRequest messagesSendRequest) {
        List<MessageInfoCommand> messageInfoCommands = dozerUtil.convertor(messagesSendRequest.getMessageInfoRequests(), MessageInfoCommand.class);
        List<Long> userIds = messagesSendRequest.getUserIds();
        List<MessageRecord> messageRecords = messageApplicationService.sendMessage(messageInfoCommands, userIds);
        return new ResponseData<>().success(dozerUtil.convertor(messageRecords, MessageRecordResponse.class));
    }

    @Override
    public ResponseData<PaginationData<MessageRecordResponse>> getQueryPageMessage(QueryPageMessageRecordRequest queryPageMessageRecordRequest) {
        IPage<MessageRecordVo> messageRecordVoIPage = messageRecordQueryRepository.queryMessageRecordPage(queryPageMessageRecordRequest);
        PaginationData<MessageRecordResponse> paginationData = dozerUtil.convertor(messageRecordVoIPage, PaginationData.class);
        paginationData.setRecords(dozerUtil.convertor(messageRecordVoIPage.getRecords(), MessageRecordResponse.class));
        return new ResponseData<>().success(paginationData);
    }

    @Override
    public ResponseData<List<MessageRecordResponse>> getQueryListMessage(QueryListMessageRecordRequest queryListMessageRecordRequest) {
        List<MessageRecordVo> messageRecordVos = messageRecordQueryRepository.queryMessageRecordList(queryListMessageRecordRequest);
        return new ResponseData<>().success(dozerUtil.convertor(messageRecordVos, MessageRecordResponse.class));
    }

    @Override
    public ResponseData<Boolean> postDeleteMessage(List<Long> ids) {
        return new ResponseData<>().success(messageApplicationService.deleteMessages(ids));
    }

    @Override
    public ResponseData<List<MessageRecordResponse>> postReadMessage(List<Long> ids) {
        return new ResponseData<>().success(dozerUtil.convertor(messageApplicationService.readMessages(ids), MessageRecordResponse.class));
    }

    @Override
    public ResponseData<MessageRecordResponse> postQueryMessage(Long id) {
        return new ResponseData<>().success(dozerUtil.convertor(messageApplicationService.queryMessages(id), MessageRecordResponse.class));
    }
}
